package com.dlc.apk.utils;

import android.text.TextUtils;

import com.dlc.apk.api.entity.AdData;
import com.dlc.apk.api.entity.DeviceInfoResponseData;
import com.dlc.apk.api.entity.UpdateVersionMQTTMsg;
import com.dlc.apk.base.BaseApp;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.Nullable;

import java.util.List;

public class CacheHelper {
    static {
        PrefUtil.init(BaseApp.Companion.getInstance());
    }

    public static void setDeviceInfo(DeviceInfoResponseData deviceInfoResponseData) {
        PrefUtil.getDefault().saveString("deviceInfo", GsonUtil.getInstance().parseObjToJsonStr(deviceInfoResponseData));
    }

    public static DeviceInfoResponseData getDeviceInfo() {
        String s = PrefUtil.getDefault().getString("deviceInfo", "{}");
        return GsonUtil.getInstance().parseJsonStrToObj(s, DeviceInfoResponseData.class);
    }

    public static void saveStaffToken(String token) {
        PrefUtil.getDefault().saveString("staffToken", token);
    }

    public static String getStaffToken() {
        return PrefUtil.getDefault().getString("staffToken", "");
    }


    public static String getSerialPortPath() {
        return PrefUtil.getDefault().getString("serialPortPath", "/dev/ttyS0");
    }

    public static void setSerialPortPath(String path) {
        PrefUtil.getDefault().saveString("serialPortPath", path);
    }

    public static void removeSerialPortPath() {
        PrefUtil.getDefault().edit().remove("serialPortPath").apply();
    }

    public static int getSerialPortBaudRate() {
        return PrefUtil.getDefault().getInt("serialPortBaudRate", 38400);
    }

    public static void setSerialPortBaudRate(int baudRate) {
        PrefUtil.getDefault().saveInt("serialPortBaudRate", baudRate);
    }

    public static void removeSerialPortBaudRate() {
        PrefUtil.getDefault().edit().remove("serialPortBaudRate").apply();
    }

    public static void saveApkVersion(UpdateVersionMQTTMsg updateVersionMQTTMsg) {
        if (updateVersionMQTTMsg == null) {
            PrefUtil.getDefault().edit().remove("saveApkVersion").apply();
            return;
        }
        PrefUtil.getDefault().saveString("saveApkVersion", GsonUtil.getInstance().parseObjToJsonStr(updateVersionMQTTMsg));
    }

    public static UpdateVersionMQTTMsg getApkVersion() {
        String s = PrefUtil.getDefault().getString("saveApkVersion", "{}");
        return GsonUtil.getInstance().parseJsonStrToObj(s, UpdateVersionMQTTMsg.class);
    }

    public static void saveHardwareVersion(UpdateVersionMQTTMsg updateVersionMQTTMsg) {
        if (updateVersionMQTTMsg == null) {
            PrefUtil.getDefault().edit().remove("saveHardwareVersion").apply();
            return;
        }
        PrefUtil.getDefault().saveString("saveHardwareVersion", GsonUtil.getInstance().parseObjToJsonStr(updateVersionMQTTMsg));
    }

    public static UpdateVersionMQTTMsg getHardwareVersion() {
        String s = PrefUtil.getDefault().getString("saveHardwareVersion", "{}");
        return GsonUtil.getInstance().parseJsonStrToObj(s, UpdateVersionMQTTMsg.class);
    }

    public static int getAdPlayTimes(@Nullable String adId) {
        if (TextUtils.isEmpty(adId)) {
            return 0;
        }
        return PrefUtil.getDefault().getInt("adPlayTimes_" + adId, 0);
    }

    public static void saveAdPlayTimes(String adId) {
        String cacheKey = "adPlayTimes_" + adId;
        if (adId == null) {
            PrefUtil.getDefault().edit().remove(cacheKey).apply();
            return;
        }
        PrefUtil.getDefault().saveInt(cacheKey, getAdPlayTimes(adId) + 1);
    }

    public static void saveAdResponseData(List<AdData> adDataList) {
        if (adDataList == null) {
            PrefUtil.getDefault().edit().remove("saveAdResponseData").apply();
            return;
        }
        PrefUtil.getDefault().saveString("saveAdResponseData", GsonUtil.getInstance().parseObjToJsonStr(adDataList));
    }

    public static List<AdData> getAdResponseData() {
        String s = PrefUtil.getDefault().getString("saveAdResponseData", "");
        if (TextUtils.isEmpty(s)) {
            return null;
        }
        return GsonUtil.getInstance().parseJsonStrToObj(s, new TypeToken<List<AdData>>() {
        }.getType());
    }

}
